package pl.diagnoser.server.persistance.dao;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import pl.diagnoser.client.dto.ObjectAttributesDTO;
import pl.diagnoser.client.dto.ObjectsDTO;
import pl.diagnoser.server.persistance.map.ObjectAttributes;
import pl.diagnoser.server.persistance.map.Objects;
import pl.diagnoser.server.persistance.tools.HibernateUtil;

public class ObjectAttributesDao implements Serializable {

	private static final long serialVersionUID = 1L;
	
	public Set<ObjectAttributesDTO> getObjectAttributes(Long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Set<ObjectAttributesDTO> objectAttributesDTO = new HashSet<ObjectAttributesDTO>(0);
		Objects object = (Objects) session.get(Objects.class, id);
		if(object != null) {
			ObjectsDTO objectDTO = new ObjectsDao().createObjectsDTO(object);
			for(ObjectAttributes objectAttributes : object.getObjectAttributes()) {			
				objectAttributesDTO.add(new ObjectAttributesDTO(objectDTO, objectAttributes.getAttribute(), objectAttributes.getAttributeValue()));
			}
		}
		
		session.getTransaction().commit();
		session.close();
		
		return objectAttributesDTO;
	}
	
	public ObjectAttributesDTO createObjectAttributesDTO( ObjectAttributes objectAttributes ) {
		ObjectsDTO object = new ObjectsDao().createObjectsDTO(objectAttributes.getObjects());
		return new ObjectAttributesDTO(object, objectAttributes.getAttribute(), objectAttributes.getAttributeValue());
	}
}
